package com.zidian.teacher.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

import com.zidian.teacher.R;
import com.zidian.teacher.bean.GroupBean;
import com.zidian.teacher.bean.StudyBean;

/**
 * Created by Administrator on 2016/9/6 0006.
 */
public class StudySelectAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<GroupBean> groupBeanList;
    private List<StudyBean> studyList = new ArrayList<>();


    public StudySelectAdapter(Context context, List<GroupBean> groupBeanList) {
        this.context = context;
        setGroupList(groupBeanList);

    }


    public void setGroupList(List<GroupBean> groupBeanList) {
        this.groupBeanList = groupBeanList;
        for (int i = 0; i < groupBeanList.size(); i++) {
            if (groupBeanList.get(i).isSelect()) {
                if (studyList != null) {
                    studyList.clear();
                    studyList.addAll(groupBeanList.get(i).getStudyBeans());
                }
            }
        }
        notifyDataSetChanged();
    }

    public int getStudyListIndex() {
        int index = 0;
        for (int i = 0; i < groupBeanList.size(); i++) {
            if (groupBeanList.get(i).isSelect()) {
                index = i;
            }
        }
        return index;
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.item_study_select_study, parent,
                false);
        return new ItemViewHolder(view);

    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof ItemViewHolder) {

            ((ItemViewHolder) holder).studyName.setText(studyList.get(position).getStudyName());
            ((ItemViewHolder) holder).studyId.setText(studyList.get(position).getStudyID());
            Glide.with(context)
                    .load(studyList.get(position).getStudyImage())
                    .placeholder(R.drawable.student)
                    .centerCrop()
                    .into(((ItemViewHolder) holder).studyImage);

            if (studyList.get(position).isSelect()) {
                ((ItemViewHolder) holder).selectImage.setBackgroundResource(R.drawable.checked);
            } else {
                ((ItemViewHolder) holder).selectImage.setBackgroundResource(R.mipmap.unchecked);

            }

            if (onItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getLayoutPosition();
                        onItemClickListener.onItemClick(holder.itemView, position);
                    }
                });

                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int position = holder.getLayoutPosition();
                        onItemClickListener.onItemLongClick(holder.itemView, position);
                        return false;
                    }
                });
            }


        }


    }

    @Override
    public int getItemCount() {
        return studyList.size() == 0 ? 0 : studyList.size();
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {

        CircleImageView studyImage;
        TextView studyName;
        TextView studyId;
        ImageView selectImage;

        public ItemViewHolder(View view) {
            super(view);
            studyImage = (CircleImageView) view.findViewById(R.id.ask_req_use_image);
            studyName = (TextView) view.findViewById(R.id.group_study_name);
            studyId = (TextView) view.findViewById(R.id.group_study_id);
            selectImage = (ImageView) view.findViewById(R.id.select_img);


        }
    }

}
